<?php
/**
 * Cache基础类
 * Date: 2025/08/31
 */

namespace App\Cache;

use Illuminate\Support\Facades\Cache;

abstract class BaseCache
{
    /**
     * 缓存Key
     * @var string
     */
    public $cacheKey;

    /**
     * 替换key中的%s
     * @date 2025/09/16
     * @param array $data 需要替换的数据
     * @return string
     */
    public function replaceKey($data)
    {
        return vsprintf($this->cacheKey, $data);
    }

    /**
     * 设置缓存
     * @date 2025/09/16
     * @param mixed $value 需要设置的值
     * @param int $expire 过期时间
     * @return bool
     */
    public function set($value, $expire = null)
    {
        return Cache::set($this->cacheKey, $value, $expire);
    }

    /**
     * 获取缓存value
     * @date 2025/09/16
     * @return mixed
     */
    public function get()
    {
        return Cache::get($this->cacheKey);
    }

    /**
     * 数值自增
     * @date 2025/09/16
     * @param int $num 操作数量
     * @return mixed
     */
    public function inc($num = 1)
    {
        return Cache::inc($this->cacheKey, $num);
    }

    /**
     * 数值自减
     * @date 2025/09/16
     * @param int $num 操作数量
     * @return mixed
     */
    public function dec($num = 1)
    {
        return Cache::dec($this->cacheKey, $num);
    }

    /**
     * 删除缓存
     * @date 2025/09/16
     * @return bool
     */
    public function delete()
    {
        return Cache::delete($this->cacheKey);
    }
}
